use crate::public::Rule;
use sonic_rs::JsonValueTrait;
use std::collections::HashSet;

pub struct Rest;

impl Rule for Rest {
    async fn get_rule(&self) -> HashSet<String> {
        let client = reqwest::Client::new();
        let resp = client
            .get("https://fapi.binance.com/fapi/v1/exchangeInfo")
            .send()
            .await
            .unwrap();

        let body = resp.text().await.unwrap();

        let array = sonic_rs::get_from_str(body.as_str(), &["symbols"])
            .unwrap()
            .into_array_iter()
            .unwrap();

        let mut set = HashSet::new();

        for symbol in array {
            let obj = symbol.unwrap();

            if obj.get("status").as_str().unwrap() != "TRADING"
                || obj.get("quoteAsset").as_str().unwrap() != "USDT"
                || obj.get("contractType").as_str().unwrap() != "PERPETUAL"
            {
                continue;
            }

            set.insert(obj.get("symbol").as_str().unwrap().to_string());
        }

        set
    }
}
